<?php

namespace App\Console\Commands;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class MakeDicsCommand extends BaseCommand
{
    /**
     * @var \Illuminate\Filesystem\Filesystem
     */
    protected $files;
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'create:dics';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = "从dic表生成Dics类文件";


    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $sql = "select * from dic order by dic_name,dic_key";
        $data = [];
        $list = DB::select($sql);
        foreach ($list as $line) {
            $matrix = json_decode(json_encode($line), true);
            $matrix["dic_name"] = str_replace(".", "_", $matrix["dic_name"]);
            $matrix["dic_key"] = str_replace(".", "_", $matrix["dic_key"]);
            $matrix["dic_name"] = strtoupper($matrix["dic_name"]);
            $matrix["dic_key"] = strtoupper($matrix["dic_key"]);
            array_push($data, $matrix);
        }

        $data = [
            'columns' => $data,
        ];

        $this->putCommonVariables($data);
        $this->doGenerate($data);
    }

    protected function doGenerate($data)
    {
        $this->files = new Filesystem();
        $basePath = app()->basePath();
        $filePath = $basePath . "/app/Utils/DicConstants.php";
        $content = view('stubs/dics', $data);
        $this->files->put($filePath, $content);
        echo $filePath . " generated\n";
    }


    protected function getArguments()
    {
        return [
            ['table', InputArgument::OPTIONAL, 'specify the table']
        ];
    }

    protected function getOptions()
    {
        return [
            ['skip', "s", InputOption::VALUE_OPTIONAL, 'skip tables matches with the rule'],
        ];
    }
}